const util = require('../../utils/util.js');//请求方法
const api = require('../../config/api.js');
Page({
  data: {
    isMember: wx.getStorageSync('userInfo').isMember,
    list: [],
    bannerList:[],
    page:1,
    totalPages:1,
    indicatorDots: true, // 是否显示面板指示点
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔（单位：ms）
    duration: 1000, // 滑动动画时长（单位：ms）
  },
  onShareAppMessage() {
    return {};
  },
  view_5OnClick(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: '/pages/ActivityDetail/ActivityDetail?id='+id});
  },
  image_1OnClick() {
    wx.navigateTo({ url: '/pages/ActivityDetail/ActivityDetail' });
  },

  onShow(options) {
    //赋值isMember(0不显示,1显示)
    const that =this
     that.banners()
    that.setData({
      isMember:wx.getStorageSync('userInfo').isMember
    })
  },
  onLoad(){
    const that =this
    // 赋值isMember(0不显示,1显示)
    that.banners()
    that.setData({
      isMember:wx.getStorageSync('userInfo').isMember
    })
    that.getIndexData();
  },
  banners(){
    const that =this
    util.request(api.bannerList,{},'POST').then(function(res) {
      if (res.errno === 200) {
        that.setData({
          bannerList: res.data ,
        });
      }else {
        // 可以考虑在这里处理错误情况，比如提示用户请求失败
        console.error('获取Banner数据失败');
      }
    }).catch((error) => {
      console.error('请求异常', error);
      // 异常处理
    });;
  },
  getIndexData() {
    let that = this;
    util.request(api.ActvityList,{pageRequestVo: {pageNum: this.data.page, pageSize: 10}},'POST').then(function(res) {
      if (res.errno === 200) {
        console.log('shouye')
        res.data.list = res.data.list.map(item => {
        //   // 假设 util.formatTime 是一个接受日期对象或时间戳并返回格式化字符串的函数
          item.timeStart =util.formatTime(new Date(item.timeStart))
          item.timeEnd =util.formatTime(new Date(item.timeEnd))
          return item;
        });
        that.setData({
          list: that.data.list.concat(res.data.list || []),
          totalPages:res.data.pages
        });
      }
    });
  },
  onPullDownRefresh() {
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  onReachBottom() {
    if ( this.data.page < this.data.totalPages ) {
      this.setData({
        page: this.data.page + 1
      });
      this.getIndexData();
    } else {
      wx.showToast({
        title: '没有更多了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  // 缴费按钮
  view_13OnClick() {
    if (wx.getStorageSync('userInfo').isMember === 1 ||wx.getStorageSync('userInfo').isMember === "1" ) {
      wx.navigateTo({url: '/pages/PayAdd/PayAdd'});
    } else {
      wx.showModal({
        title: '提示',
        content: '您当前不是会员，无法缴费，请重新登录后完善个人信息。如需入会请联系021-62581170！',
        showCancel: false,
        confirmText: '确定'
      });
    }
  },

  toActivity(){
wx.navigateTo({
  url: '/pages/activityList/activityList',
})
  }

});
